package com.jielihaofeng.dynamicproxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Proxy;

/**
 * @author Johnnie Wind
 * @description 斯诺克动态代理
 * @date 2021/11/22 09:59
 */
@Slf4j
public class SnookerProxy {
    public static void main(String[] args) {
        SnookerImpl snooker = new SnookerImpl();
        Snooker snookerProxy = getSnookerProxy(snooker, Snooker.class);
        snookerProxy.topSpin("红");
        snookerProxy.pushShot("黑");
    }

    public static <T> T getSnookerProxy(final Object snooker, Class<T> proxyClass) {
        ClassLoader loader = proxyClass.getClassLoader();
        Class<?>[] interfaces = new Class[]{proxyClass};
        Object o = Proxy.newProxyInstance(loader, interfaces, (proxy, method, args) -> {
            Object invoke = method.invoke(snooker, args);
            return invoke;
        });
        return (T) o;
    }
}
